﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using hpcpdi.DataAccess.Impl;
using hpcpdi.DataAccess.Interface;
using hpcpdi.Entities;
using hpcpdi.Infrastructure;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace hpcpdi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class TrafficController : ControllerBase
    {
        private readonly ITrafficRepository _trafficRepository;
        public TrafficController(ITrafficRepository trafficRepository)
        {
            _trafficRepository = trafficRepository;
        }
        /// <summary>
        ///  获取今日通行统计量数据
        /// </summary>
        /// <returns></returns>
        [HttpGet("/api/traffic/GetDailyTrafficVolume")]
        public async Task<ResultModel<daily_traffic_volume>> GetDailyTrafficVolume(DateTime date)
        {
            if (date ==DateTime.Parse("0001/1/1 0:00:00"))
            {
                date = Convert.ToDateTime(DateTime.Now.ToShortDateString());
            }
            return ResultModel.Success<daily_traffic_volume>(await _trafficRepository.GetDailyTrafficVolume(date));                
        }

        [HttpPost("/api/traffic/CreateUpdate")]
        public ResultModel<int> CreateUpdateTrafficVolume(DateTime tDate, int trafficVolume)        
        {
            daily_traffic_volume dtv = new daily_traffic_volume();
            dtv.tDate = tDate;
            dtv.trafficVolume = trafficVolume;

            var model = _trafficRepository.GetDailyTrafficVolume(tDate);
            //如果不存在则新增数据
            if ( model== null)
            {
                if (_trafficRepository.CreateDailyTrafficVolume(dtv))
                {
                   return  ResultModel.Success<int>(1);
                }
                else
                {
                    return ResultModel.Success<int>(0);

                }
            }
            else
            {                
                if (_trafficRepository.UpdateDailyTrafficVolume(dtv))
                {
                    return ResultModel.Success<int>(1);
                }
                return ResultModel.Success<int>(0);
            }
            
            
        }
    }
}
